﻿using System.Collections.Generic;
using System.Linq;
using System.Data.SqlClient;
using System.Configuration;

public class TestResultAccess
{
    public static TestResultType1Report SelectType1(string tel, string year, string code)
    {
        TestResultType1Report result = new TestResultType1Report();
        string connectionName = string.Format("ConnectionString{0}", year.Replace("/", ""));
        DataClassesDataContext dataCotext = new DataClassesDataContext(ConfigurationManager.ConnectionStrings[connectionName].ConnectionString);             
        var list = from okTestViews in dataCotext.OkTest1Views.Where(
                        item => (item.pre_code == code && item.tel == tel && item.datein.StartsWith(year)))
                   select new
                   {
                       okTestViews.name,
                       okTestViews.sex,
                       okTestViews.age,
                       okTestViews.datein,
                       okTestViews.Resdate,
                       okTestViews.nametest,
                       okTestViews.result,
                       okTestViews.pre_code,
                       okTestViews.unit,
                       okTestViews.docsex,
                       okTestViews.docname,
                       okTestViews.Decsg,
                       okTestViews.Sname,
                       okTestViews.Scode

                   };

        foreach (var item in list)
        {
            result.Rows.Add(item.name,
                         item.sex,
                         item.age,
                         item.datein,
                         item.Resdate,
                         item.nametest,
                         item.result,
                         item.pre_code,
                         item.unit,
                         item.docsex,
                         item.docname,
                         item.Decsg,
                         item.Sname,
                         item.Scode);
        }
        return result;
    }
    public static TestResultType2Report SelectType2(string tel, string year, string code)
    {
        TestResultType2Report result = new TestResultType2Report();
        string connectionName = string.Format("ConnectionString{0}", year.Replace("/", ""));
        DataClassesDataContext dataCotext = new DataClassesDataContext(ConfigurationManager.ConnectionStrings[connectionName].ConnectionString);      
        var list = from okTestViews in dataCotext.OkTest2Views.Where(
                        item => (item.pre_code == code && item.tel == tel && item.datein.StartsWith(year)))
                   select new
                   {
                       okTestViews.uniqcode,
                       okTestViews.codetest,
                       okTestViews.nametest,
                       okTestViews.codesec,
                       okTestViews.titrmic,
                       okTestViews.javabmic,
                       okTestViews.normmic,
                       okTestViews.Sname,
                       okTestViews.Scode,
                       okTestViews.pre_code,
                       okTestViews.sex,
                       okTestViews.age,
                       okTestViews.datein,
                       okTestViews.Resdate,
                       okTestViews.docname,
                       okTestViews.name
                   };

        foreach (var item in list)
        {
            result.Rows.Add(
                       item.uniqcode,
                       item.codetest,
                       item.nametest,
                       item.codesec,
                       item.titrmic,
                       item.javabmic,
                       item.normmic,
                       item.Sname,
                       item.Scode,
                       item.pre_code,
                       item.sex,
                       item.age,
                       item.datein,
                       item.Resdate,
                       item.docname,
                       item.name);
        }
        return result;
    }
}